package com.ias.agdis_r.activity.setting;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseKeyActivity;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.netservice.actions.PrivateNetService;
import com.ias.agdis_r.netservice.actions.PublicNetService;
import com.ias.agdis_r.netservice.actions.WifiNetService;
import com.ias.agdis_r.util.S;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IP地址设置界面
 * @author zhangsc
 *
 */
@SuppressLint("ResourceAsColor")
public class IPSettingActivity extends BaseKeyActivity implements OnClickListener{

	private ActionBar actionBar;
	private TextView txt_net_pri,txt_net_pub,txt_net_wifi;
	private View view_net_pri,view_net_pub,view_net_wifi;
	private List<TextView> menuList = new ArrayList<TextView>();
	private List<View> viewList = new ArrayList<View>();
	/** 专网设置控件*/
	private EditText edit_pri_ip_restful,edit_pri_port_restful,edit_pri_port_push,edit_pri_ip_push;
	/** 公网设置控件*/
	private EditText edit_pub_ip_restful,edit_pub_port_restful,edit_pub_port_push,edit_pub_ip_push;
	/** WIFI设置控件*/
	private EditText edit_wifi_ssid,edit_wifi_pwd,edit_wifi_ip_restful,edit_wifi_port_restful,edit_wifi_port_push,edit_wifi_ip_push;
	/** 配置变化标识*/
	private boolean isConfigChange = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ipsetting);
		actionBar = getActionBar();
		initActionBar();
		txt_net_pri = (TextView)findViewById(R.id.txt_net_private);
		txt_net_pub = (TextView)findViewById(R.id.txt_net_public);
		txt_net_wifi = (TextView)findViewById(R.id.txt_net_wifi);
		
		view_net_pri = findViewById(R.id.view_net_private);
		view_net_pub = findViewById(R.id.view_net_public);
		view_net_wifi = findViewById(R.id.view_net_wifi);
		
		txt_net_pri.setOnClickListener(this);
		txt_net_pub.setOnClickListener(this);
		txt_net_wifi.setOnClickListener(this);
		findViewById(R.id.actionbar_settings_back).setOnClickListener(this);
		
		menuList.add(txt_net_pri);
		menuList.add(txt_net_pub);
		menuList.add(txt_net_wifi);
		
		viewList.add(view_net_pri);
		viewList.add(view_net_pub);
		viewList.add(view_net_wifi);
		initNetControl();
		loadData();
		
		Intent intent = getIntent();
		if(intent!=null&&intent.getExtras()!=null)
		{
			String type = intent.getExtras().getString("type");
			if(S.isEmpty(type))
			{
				return;
			}
			else if(type.equals(PublicNetService.class.getName())){
				loadView(txt_net_pub,view_net_pub);
			}
			else if(type.equals(PrivateNetService.class.getName())){
				loadView(txt_net_pri,view_net_pri);
			}
			else if(type.equals(WifiNetService.class.getName())){
				loadView(txt_net_wifi,view_net_wifi);
			}
		}
	}
	
	@SuppressLint("InflateParams")
	private void initActionBar() {
		actionBar.setCustomView(LayoutInflater.from(IPSettingActivity.this)
				.inflate(R.layout.actionbar_settings, null));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
		TextView title = (TextView) findViewById(R.id.txt_title);
		title.setText(R.string.IPSettingActivity);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.txt_net_private:
				loadView(txt_net_pri,view_net_pri);
				break;
			case R.id.txt_net_public:
				loadView(txt_net_pub,view_net_pub);
				break;
			case R.id.txt_net_wifi:
				loadView(txt_net_wifi,view_net_wifi);
				break;
			case R.id.actionbar_settings_back:
				//如果修改了配置，弹出确认窗口
				if(isConfigChange){
					openConfirmWindow();
				}else{
					//退出设置界面
					finish();
				}
				break;
		}
	}


	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK) 
		{
			//如果修改了配置，弹出确认窗口
			if(isConfigChange){
				openConfirmWindow();
			}else{
				//退出设置界面
				finish();
			}
		}
		return false;
	}
	/**
	 * 校验输入数据
	 */
	private void checkData()
	{
		/** 专网 */
		String pri_ip_restful = S.valueOf(edit_pri_ip_restful.getText());
		String pri_port_restful = S.valueOf(edit_pri_port_restful.getText());
		String pri_ip_push = S.valueOf(edit_pri_ip_push.getText());
		String pri_port_push = S.valueOf(edit_pri_port_push.getText());
		
		/** 公网 */
		String pub_ip_restful = S.valueOf(edit_pub_ip_restful.getText());
		String pub_port_restful = S.valueOf(edit_pub_port_restful.getText());
		String pub_ip_push = S.valueOf(edit_pub_ip_push.getText());
		String pub_port_push = S.valueOf(edit_pub_port_push.getText());
		
		/** WIFI */
//		String wifi_ssid = S.valueOf(edit_wifi_ssid.getText());
//		String wifi_pwd = S.valueOf(edit_wifi_pwd.getText());
		String wifi_ip_restful = S.valueOf(edit_wifi_ip_restful.getText());
		String wifi_port_restful = S.valueOf(edit_wifi_port_restful.getText());
		String wifi_ip_push = S.valueOf(edit_wifi_ip_push.getText());
		String wifi_port_push = S.valueOf(edit_wifi_port_push.getText());
		
		if(S.isNotEmpty(pri_ip_restful)&&!S.isIP(pri_ip_restful)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE,
					getString(R.string.pri_label)+getString(R.string.illegal_ip_restful), false);
			loadView(txt_net_pri,view_net_pri);
			edit_pri_ip_restful.requestFocus();
		}else if(S.isNotEmpty(pri_port_restful)&&!S.isPort(pri_port_restful)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE,
					getString(R.string.pri_label)+getString(R.string.illegal_port_restful), false);
			loadView(txt_net_pri,view_net_pri);
			edit_pri_port_restful.requestFocus();
		}else if(S.isNotEmpty(pri_ip_push)&&!S.isIP(pri_ip_push)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.pri_label)+getString(R.string.illegal_ip_push), false);
			loadView(txt_net_pri,view_net_pri);
			edit_pri_ip_push.requestFocus();
		}else if(S.isNotEmpty(pri_port_push)&&!S.isPort(pri_port_push)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.pri_label)+getString(R.string.illegal_port_push), false);
			loadView(txt_net_pri,view_net_pri);
			edit_pri_port_push.requestFocus();
		}else if(S.isNotEmpty(pub_ip_restful)&&!S.isIP(pub_ip_restful)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.pub_label)+getString(R.string.illegal_ip_restful), false);
			loadView(txt_net_pub,view_net_pub);
			edit_pub_ip_restful.requestFocus();
		}else if(S.isNotEmpty(pub_ip_push)&&!S.isIP(pub_ip_push)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE,
					getString(R.string.pub_label)+getString(R.string.illegal_ip_push), false);
			loadView(txt_net_pub,view_net_pub);
			edit_pub_ip_push.requestFocus();
		}else if(S.isNotEmpty(pub_port_restful)&&!S.isPort(pub_port_restful)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE,
					getString(R.string.pub_label)+getString(R.string.illegal_port_restful), false);
			loadView(txt_net_pub,view_net_pub);
			edit_pub_port_restful.requestFocus();
		}else if(S.isNotEmpty(pub_port_push)&&!S.isPort(pub_port_push)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.pub_label)+getString(R.string.illegal_port_push), false);
			loadView(txt_net_pub,view_net_pub);
			edit_pub_port_push.requestFocus();
		}else if(S.isNotEmpty(wifi_ip_restful)&&!S.isIP(wifi_ip_restful)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.wifi_label)+getString(R.string.illegal_ip_restful), false);
			loadView(txt_net_wifi,view_net_wifi);
			edit_wifi_ip_restful.requestFocus();
		}else if(S.isNotEmpty(wifi_port_restful)&&!S.isPort(wifi_port_restful)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE,
					getString(R.string.wifi_label)+getString(R.string.illegal_port_restful), false);
			loadView(txt_net_wifi,view_net_wifi);
			edit_wifi_port_restful.requestFocus();
		}else if(S.isNotEmpty(wifi_ip_push)&&!S.isIP(wifi_ip_push)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.wifi_label)+getString(R.string.illegal_ip_push), false);
			loadView(txt_net_wifi,view_net_wifi);
			edit_wifi_ip_push.requestFocus();
		}else if(S.isNotEmpty(wifi_port_push)&&!S.isPort(wifi_port_push)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, 
					getString(R.string.wifi_label)+getString(R.string.illegal_port_push), false);
			loadView(txt_net_wifi,view_net_wifi);
			edit_wifi_port_push.requestFocus();
		}else{

			saveData();
			finish();
		}
	}

	/**
	 * 弹出保存确认窗口
	 * **/
	public void openConfirmWindow() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle(getString(R.string.save_config)).setIcon(android.R.drawable.ic_dialog_info)
				.setMessage(getString(R.string.alert_save_config)).setPositiveButton(
				getString(R.string.save), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
										int which) {
						checkData();
					}

				}).setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).create().show();
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		loadData();
	}

	private void initNetControl()
	{
		/** 专网 */
		edit_pri_ip_restful = (EditText) view_net_pri.findViewById(R.id.edit_ip_restful);
		edit_pri_port_restful = (EditText) view_net_pri.findViewById(R.id.edit_port_restful);
		edit_pri_ip_push = (EditText) view_net_pri.findViewById(R.id.edit_ip_push);
		edit_pri_port_push = (EditText) view_net_pri.findViewById(R.id.edit_port_push);

		/** 公网 */	
		edit_pub_ip_restful = (EditText) view_net_pub.findViewById(R.id.edit_ip_restful);
		edit_pub_port_restful = (EditText) view_net_pub.findViewById(R.id.edit_port_restful);
		edit_pub_ip_push = (EditText) view_net_pub.findViewById(R.id.edit_ip_push);
		edit_pub_port_push = (EditText) view_net_pub.findViewById(R.id.edit_port_push);
		
		/** WIFI */
		edit_wifi_ssid= (EditText) view_net_wifi.findViewById(R.id.edit_ssid);
		edit_wifi_pwd= (EditText) view_net_wifi.findViewById(R.id.edit_password);
		edit_wifi_ip_restful = (EditText) view_net_wifi.findViewById(R.id.edit_ip_restful);
		edit_wifi_port_restful = (EditText) view_net_wifi.findViewById(R.id.edit_port_restful);
		edit_wifi_ip_push = (EditText) view_net_wifi.findViewById(R.id.edit_ip_push);
		edit_wifi_port_push = (EditText) view_net_wifi.findViewById(R.id.edit_port_push);

		edit_pri_ip_restful.addTextChangedListener(new MyTextWatcher());
		edit_pri_port_restful.addTextChangedListener(new MyTextWatcher());
		edit_pri_ip_push.addTextChangedListener(new MyTextWatcher());
		edit_pri_port_push.addTextChangedListener(new MyTextWatcher());

		edit_pub_ip_restful.addTextChangedListener(new MyTextWatcher());
		edit_pub_port_restful.addTextChangedListener(new MyTextWatcher());
		edit_pub_ip_push.addTextChangedListener(new MyTextWatcher());
		edit_pub_port_push.addTextChangedListener(new MyTextWatcher());

		edit_wifi_ssid.addTextChangedListener(new MyTextWatcher());
		edit_wifi_pwd.addTextChangedListener(new MyTextWatcher());
		edit_wifi_ip_restful.addTextChangedListener(new MyTextWatcher());
		edit_wifi_port_restful.addTextChangedListener(new MyTextWatcher());
		edit_wifi_ip_push.addTextChangedListener(new MyTextWatcher());
		edit_wifi_port_push.addTextChangedListener(new MyTextWatcher());
	}

	class MyTextWatcher implements TextWatcher {

		private String beforeText = "";
		/** 初始化标记，初始化成功后，设为true*/
		private boolean initFlag = false;

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			if(initFlag) {
				this.beforeText = s.toString();
			}
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

			if(!initFlag) {
				initFlag = true;
			}
			else if(!this.beforeText.equals(s.toString())){
				isConfigChange = true;
				Log.i("AGDIS", "IP地址设置发生变化");
			}

		}

		@Override
		public void afterTextChanged(Editable s) {


		}
	}
	/**
	 * 加载内容
	 */
	private void loadData()
	{
		/** 专网 */		
		String pri_ip_restful = DataBaseManager.getInstance().getString(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_IP_RESTFUL);
		String pri_port_restful = DataBaseManager.getInstance().getString(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_PORT_RESTFUL);
		String pri_ip_push = DataBaseManager.getInstance().getString(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_IP_PUSH);
		String pri_port_push = DataBaseManager.getInstance().getString(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_PORT_PUSH);
		
		edit_pri_ip_restful.setText(pri_ip_restful);
		edit_pri_port_restful.setText(pri_port_restful);
		edit_pri_ip_push.setText(pri_ip_push);
		edit_pri_port_push.setText(pri_port_push);
		
		/** 公网 */	
		String pub_ip_restful = DataBaseManager.getInstance().getString(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_IP_RESTFUL);
		String pub_port_restful = DataBaseManager.getInstance().getString(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_PORT_RESTFUL);
		String pub_ip_push = DataBaseManager.getInstance().getString(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_IP_PUSH);
		String pub_port_push = DataBaseManager.getInstance().getString(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_PORT_PUSH);
		
		edit_pub_ip_restful.setText(pub_ip_restful);
		edit_pub_port_restful.setText(pub_port_restful);
		edit_pub_ip_push.setText(pub_ip_push);
		edit_pub_port_push.setText(pub_port_push);
		
		/** WIFI */
		String wifi_ssid = DataBaseManager.getInstance().getString(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_SSID);
		String wifi_pwd = DataBaseManager.getInstance().getString(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_PASSWORD);
		String wifi_ip_restful = DataBaseManager.getInstance().getString(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_IP_RESTFUL);
		String wifi_port_restful = DataBaseManager.getInstance().getString(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_PORT_RESTFUL);
		String wifi_ip_push = DataBaseManager.getInstance().getString(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_IP_PUSH);
		String wifi_port_push = DataBaseManager.getInstance().getString(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_PORT_PUSH);

		edit_wifi_ssid.setText(wifi_ssid);
		edit_wifi_pwd.setText(wifi_pwd);
		edit_wifi_ip_restful.setText(wifi_ip_restful);
		edit_wifi_port_restful.setText(wifi_port_restful);
		edit_wifi_ip_push.setText(wifi_ip_push);
		edit_wifi_port_push.setText(wifi_port_push);
	}
	
	/**
	 * 保存数据
	 */
	private void saveData()
	{
		/** 专网 */
		String pri_ip_restful = S.valueOf(edit_pri_ip_restful.getText());
		String pri_port_restful = S.valueOf(edit_pri_port_restful.getText());
		String pri_ip_push = S.valueOf(edit_pri_ip_push.getText());
		String pri_port_push = S.valueOf(edit_pri_port_push.getText());
		
		DataBaseManager.getInstance().insert(ParameterModel.T_PRIVATE_INFO,
			ParameterModel.NET_IP_RESTFUL,pri_ip_restful);
		DataBaseManager.getInstance().insert(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_PORT_RESTFUL,pri_port_restful);
		DataBaseManager.getInstance().insert(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_IP_PUSH,pri_ip_push);
		DataBaseManager.getInstance().insert(ParameterModel.T_PRIVATE_INFO,
				ParameterModel.NET_PORT_PUSH,pri_port_push);
		
		/** 公网 */
		String pub_ip_restful = S.valueOf(edit_pub_ip_restful.getText());
		String pub_port_restful = S.valueOf(edit_pub_port_restful.getText());
		String pub_ip_push = S.valueOf(edit_pub_ip_push.getText());
		String pub_port_push = S.valueOf(edit_pub_port_push.getText());
		
		DataBaseManager.getInstance().insert(ParameterModel.T_PUBLIC_INFO,
			ParameterModel.NET_IP_RESTFUL,pub_ip_restful);
		DataBaseManager.getInstance().insert(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_PORT_RESTFUL,pub_port_restful);
		DataBaseManager.getInstance().insert(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_IP_PUSH,pub_ip_push);
		DataBaseManager.getInstance().insert(ParameterModel.T_PUBLIC_INFO,
				ParameterModel.NET_PORT_PUSH,pub_port_push);
		
		/** WIFI */
		String wifi_ssid = S.valueOf(edit_wifi_ssid.getText());
		String wifi_pwd = S.valueOf(edit_wifi_pwd.getText());
		String wifi_ip_restful = S.valueOf(edit_wifi_ip_restful.getText());
		String wifi_port_restful = S.valueOf(edit_wifi_port_restful.getText());
		String wifi_ip_push = S.valueOf(edit_wifi_ip_push.getText());
		String wifi_port_push = S.valueOf(edit_wifi_port_push.getText());
		
		DataBaseManager.getInstance().insert(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_SSID,wifi_ssid);
		DataBaseManager.getInstance().insert(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_PASSWORD,wifi_pwd);
		DataBaseManager.getInstance().insert(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_IP_RESTFUL,wifi_ip_restful);
		DataBaseManager.getInstance().insert(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_PORT_RESTFUL,wifi_port_restful);
		DataBaseManager.getInstance().insert(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_IP_PUSH,wifi_ip_push);
		DataBaseManager.getInstance().insert(ParameterModel.T_WIFI_INFO,
				ParameterModel.NET_PORT_PUSH,wifi_port_push);
		
		EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, getString(R.string.saveIP), false);
		
		//关闭网络连接，重新检测
		ServiceManage.getInstance().closeAllService();
	}
	
	/**
	 * 更新显示
	 * @param selectMenu
	 * @param selectView
	 */
	private void loadView(TextView selectMenu,View selectView)
	{
		if(selectMenu==null||selectView==null)
		{
			return;
		}
		for(TextView menu:menuList)
		{
			if(menu == null)
			{
				continue;
			}
			else if(menu == selectMenu)
			{
				menu.setTextColor(getResources().getColor(R.color.green));
				menu.setBackgroundColor(getResources().getColor(R.color.white));
			}
			else
			{
				menu.setTextColor(getResources().getColor(R.color.black));
				menu.setBackgroundColor(getResources().getColor(R.color.gray2));
			}
		}
		
		for(View view:viewList)
		{
			if(view == null)
			{
				continue;
			}
			else if(view==selectView)
			{
				view.setVisibility(View.VISIBLE);
			}
			else
			{
				view.setVisibility(View.GONE);
			}
		}
	}
	
}
